package com.winchain.chainbill.bill.biz.transfer.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.service.CbRcvService;
import com.winchain.chainbill.bill.biz.Bill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRcv4PrntCmd<T> implements ICommand<Bill<T>>, ICallback<Bill<T>, AcctTxRec> {
	@Autowired
	private CbRcvService cbRcvService;

	public boolean execute(CommandContext<Bill<T>> context) throws Exception {
		CbRec record = ((Bill) context.getSource()).getRec();

		CbRcv intput = getRcv(record);
		this.cbRcvService.saveOrUpdate(intput);
		return false;
	}

	public boolean callback(CallbackContext<Bill<T>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}

	private CbRcv getRcv(CbRec record) {
		CbRcv cbRcv = new CbRcv();
		cbRcv.setBillId(record.getBillId());
		cbRcv.setRcvState("2");

		cbRcv = (CbRcv) this.cbRcvService.selectOne(cbRcv);
		if (cbRcv == null) {
			cbRcv = new CbRcv();
			cbRcv.setBillId(record.getPrntId());
			cbRcv.setRcvState("2");
			cbRcv = (CbRcv) this.cbRcvService.selectOne(cbRcv);
			cbRcv.setRcvId(SystemUtil.getUUID());
			cbRcv.setBillId(record.getBillId());
			cbRcv.setOptSeq(Integer.valueOf(0));
			cbRcv.setRemark("������������������");

			cbRcv.setCreateTime(DateUtil.getSystemDateTime());
			cbRcv.setTxId(null);
		}
		return cbRcv;
	}
}
